package com.niodata.dp.task.service;

import com.niodata.dp.task.az.FlowExecuteResponse;
import com.niodata.dp.task.az.FlowScheduleException;
import com.niodata.dp.task.entity.FlowExecutionHistory;
import com.niodata.dp.task.entity.FlowExecutor;
import com.niodata.dp.task.entity.JobExecData;
import com.niodata.dp.task.entity.JobInfo;
import com.niodata.dp.task.entity.JobLog;
import com.niodata.dp.task.entity.JobState;
import com.niodata.dp.task.entity.TaskExecution;
import com.niodata.dp.task.entity.TaskExecutionDetail;
import com.niodata.dp.task.entity.TaskInfo;
import com.niodata.dp.task.entity.TaskSchedule;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * Responsible for all tasks management,create task package, communicate with azkaban for task
 * operations: submit task,execute task,schedule task,get task status,get job status.
 */
public interface TaskManager {

  public void createTask(TaskInfo taskInfo) throws Exception;

  /**
   * delete task job,only last job could be deleted.
   *
   * @param taskId taskid
   * @param jobId jobid
   */
  public void deleteJobInfo(String taskId, String jobId);

  /**
   * delete task .
   *
   * @param taskId taskid
   */
  public TaskInfo deleteTaskById(String taskId);

  /**
   * update task.
   *
   * @param taskInfo taskInfo
   */
  public void updateTask(TaskInfo taskInfo);


  /**
   * 1.find azkaban project id by taskId 2.execute azkaban project(azkaban project id).
   *
   * @param taskId task id in dp-ng
   */
  public FlowExecuteResponse executeTask(String taskId);

  /**
   * exec task by taskName and dynamic config id.
   *
   * @param taskName taskName
   * @param configId dynamic config id
   */
  public FlowExecuteResponse executeTask(String taskName, String configId);


  /**
   * 1.find azkaban project id by taskId 2.schedule azkaban project by cron.
   *
   * @param taskId task id in dp-ng
   */
  public TaskInfo scheduleTask(String taskId) throws FlowScheduleException;

  /**
   * get task scheduler next exec time,if not scheduled,return empty string.
   *
   * @param taskId taskid
   * @return next exec time string
   */
  public String getNextExecuteTime(String taskId);

  /**
   * get task schedule info,return null if not scheduled.
   *
   * @param taskId task id
   * @return TaskSchedule
   */
  public TaskSchedule getTaskSchedule(String taskId);

  /**
   * cancel task schedule.
   *
   * @param taskId task id
   */
  public TaskInfo unscheduleTask(String taskId);


  /**
   * get task execution status.
   *
   * @return taskStatus
   */
  public TaskExecutionDetail getTaskExecutionDetail(int execId);

  /**
   * get task executions.
   *
   * @param taskId task id
   * @param offset index offset
   * @param limit max count
   * @return task execution list
   */
  public List<TaskExecution> getTaskExecutions(String taskId, int offset, int limit);

  /**
   * get task executions.
   *
   * @param taskId task id
   * @param offset index offset
   * @param limit max count
   * @param status status,sperated by comma,like SUCCESSED,KILLED,RUNNING etc.
   * @param startTime startTime
   * @param endTime endTime
   * @return task execution list
   */
  public List<TaskExecution> getTaskExecutions(String taskId, int offset, int limit, String status,
      Date startTime, Date endTime);

  /**
   * get all running executions of the task.
   *
   * @param taskId task id
   * @return all running executions of the task
   */
  public List<TaskExecution> getRunningExecutions(String taskId);

  /**
   * get all running executions.
   *
   * @return running executions
   */
  public List<TaskExecution> getAllRunningExecutions(String groupName);

  /**
   * list tasks owned by the group.
   *
   * @param groupId group id
   * @param offset index offset
   * @param count max count
   * @return list of task info,both scheduled and not scheduled
   */
  public List<TaskInfo> getTasks(long groupId, int offset, int count);

  /**
   * list jobs owned by the task.
   *
   * @param taskId taskId
   * @return list of job info for task
   */
  public List<JobInfo> getJobsOfTask(String taskId);

  /**
   * list jobs owned by the task name.
   *
   * @param taskname taskname
   * @return list of job info for task by task name
   */
  public List<JobInfo> getJobsOfTaskByName(String taskname);

  public TaskInfo getTaskById(String taskid);

  public TaskInfo getTaskByName(String name);

  public void deleteTaskByName(String name);


  /**
   * stop a task execution.
   *
   * @param executionId execution id
   */
  public void stopExecution(int executionId);


  /**
   * get task execution log.
   *
   * @param taskExecutionId task execution id
   * @param offset offset
   * @param maxLength max length of log data
   * @return job execution log
   */
  public JobLog getTaskExecutionLog(int taskExecutionId, String jobname, int offset, int maxLength);

  public void submitTask(String taskId) throws IOException;

  public void reSubmitTask(String taskId) throws IOException;

  public List<FlowExecutionHistory> getExecutionHistory(int offset, int count);

  /**
   * get user's tasks.
   *
   * @param userId userid
   * @param keyWord keyword
   * @param appId appid
   * @param offset offset count
   * @param count limit count
   * @param scheduled scheduled 已调度,noscheduled 未调度,空 所有流程
   * @return task list
   */
  public List<TaskInfo> getUserTasks(long userId, String keyWord, String appId,
      int offset, int count, String scheduled);

  /**
   * get task of task user accessible.
   *
   * @param userId userid
   * @param groupId groupid
   * @param keyWord name or desc keyword
   * @param offset start from 0
   * @param count limit count
   * @param scheduled scheduled 已调度,noscheduled 未调度,空 所有流程
   * @return task list
   */
  public List<TaskInfo> getGroupTasks(long userId, long groupId, String appId, String keyWord,
      int offset, int count, String scheduled);

  /**
   * get tasks user accessible.
   *
   * @param userId userid
   * @param keyWord name or desc keyword
   * @param appId appid
   * @param offset start from 0
   * @param count limit count
   * @param scheduled scheduled 已调度,noscheduled 未调度,空 所有流程
   * @return task list
   */
  public List<TaskInfo> getUserAuthorizedTasks(long userId, String appId, String keyWord,
      int offset, int count, String scheduled);

  /**
   * get task info with jod detais.
   *
   * @param taskId task id
   * @return task info
   */
  public TaskInfo getTaskDetailInfo(String taskId);


  /**
   * get task info with jod detais.
   *
   * @param taskName task name
   * @return task info
   */
  public TaskInfo getTaskDetailInfoByName(String taskName);


  /**
   * update task detail info.
   *
   * @param taskInfo new task info
   */
  public void updateTaskDetail(TaskInfo taskInfo);


  public List<TaskInfo> findTaskByNameKeyWord(Long userId, String nameKeyWord);

  public List<TaskInfo> findTaskByApp(Long userId, String appId, String nameKeyWord);

  public void saveJobExecData(JobExecData execData);

  public void updateJobExecData(JobExecData execData);

  public List<JobExecData> getJobExecDataOfTask(String taskId, int execId);

  public JobExecData getJobExecData(int execId, String jobname);

  public List<FlowExecutor> getFlowExecutors() throws IOException;

  public JobState getJobState(String taskName, String jobName, String scheduleId);

  public void updateJobState(JobState jobState);

  public void saveJobState(JobState jobState);

  /**
   * get task count .
   *
   * @param ids group ids
   * @param scheduled isscheduled. the value can be 0、1 or null
   * @return task count
   */
  public Integer getTaskCount(Long ownerId, List<Long> ids, Integer scheduled);

}
